package com.mapforge.gui.frames.tileset;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.mapforge.Constants;
import com.mapforge.listener.TilesetEditListener;
import com.mapforge.map.Tileset;

public class NewTilesetDialog extends JDialog {
	/**
	 * Default serial version ID, here to remove warnings
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Listeners interested in tileset edits.
	 */
	private List<TilesetEditListener> editListeners;
	private JButton okBtn;
	private JButton cancelBtn;
	private JPanel btnControlPanel;

	private TilesetPropertiesPanel tilesetPropertiesPanel;

	private PropertiesActionListener propertiesActionListener;

	protected NewTilesetDialog(JFrame parent, Tileset tileset, List<TilesetEditListener> tilesetEditListeners) {
		super(parent, "Create New Tileset");
		this.editListeners = tilesetEditListeners;

		// layout GUI
		initialize();
	}

	protected void initialize() {
		propertiesActionListener = new PropertiesActionListener();
		okBtn = new JButton("OK");
		okBtn.addActionListener(propertiesActionListener);
		cancelBtn = new JButton("Cancel");
		cancelBtn.addActionListener(propertiesActionListener);

		btnControlPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 2, 2));
		btnControlPanel.add(okBtn);
		btnControlPanel.add(cancelBtn);

		tilesetPropertiesPanel = new TilesetPropertiesPanel(null,
				"Create New Tileset Panel");

		add(btnControlPanel, BorderLayout.SOUTH);
		add(tilesetPropertiesPanel, BorderLayout.CENTER);
	}

	private Tileset createNewTileset() {
		// store current tileset, inform all panels of the change
		Tileset tileset = new Tileset("Untitled",
				Constants.DEFAULT_TILE_DIMENSION.width,
				Constants.DEFAULT_TILE_DIMENSION.height);
		tilesetPropertiesPanel.setTileset(tileset);
		tilesetPropertiesPanel.reset();
		pack();
		setLocationRelativeTo(getParent());
		super.setVisible(true);
		// return new tileset
		return tileset;
	}

	public void setVisible(boolean value) {
		if (value) {
			createNewTileset();
		} else {
			super.setVisible(value);
		}
	}

	private class PropertiesActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Object obj = e.getSource();
			if (obj == cancelBtn) {
				setVisible(false);
			} else if (obj == okBtn) {

			}
		}
	}

	/*
	 * public void applyTilesetChanges() { int tileW =
	 * tileWidthModel.getNumber().intValue(); int tileH =
	 * tileHeightModel.getNumber().intValue(); String imagePath =
	 * txtTilesetImgPath.getText();
	 * 
	 * // whether changes in properties occurred boolean imgChange =
	 * !tileset.getImagePath().equals( txtTilesetImgPath.getText()); boolean
	 * tileDimChange = tileset.getTileWidth() != tileW ||
	 * tileset.getTileHeight() != tileH; boolean nameChange =
	 * txtTilesetName.getText().equals(tileset.getName());
	 * 
	 * // need to reload image if (imgChange || tileDimChange) { File imageFile
	 * = new File(imagePath); // attempt to load image if (imageFile.exists()) {
	 * try { BufferedImage img = ImageUtil.getImage(imageFile.toURI() .toURL());
	 * tileset.setImages(img, tileW, tileH); } catch (IOException e2) {
	 * JOptionPane .showMessageDialog(NewTilesetDialog.this,
	 * "Unable to load and set tileset tiles using specified image."); return; }
	 * } // ERROR: couldn't find image file. else { if (imagePath.length() > 20)
	 * { imagePath = imagePath.substring(0, 17) + "..."; }
	 * JOptionPane.showMessageDialog(NewTilesetDialog.this,
	 * "Couldn't locate image file \"" + imagePath + "\""); return; } }
	 * 
	 * // add all edits to the edit list if they've occured
	 * List<AbstractTilesetEdit> edits = new ArrayList<AbstractTilesetEdit>();
	 * if (nameChange) { edits.add(new TilesetNameEdit(tileset,
	 * tileset.getName()));
	 * tileset.setName(NewTilesetDialog.this.txtTilesetName.getText()); } if
	 * (tileset.getEmptyTileIndex() != emptyTileIndexModel.getNumber()
	 * .intValue()) { edits.add(new TilesetEmptyIndexEdit(tileset, tileset
	 * .getEmptyTileIndex()));
	 * tileset.setEmptyTileIndex(emptyTileIndexModel.getNumber() .intValue()); }
	 * if (tileDimChange && !imgChange) { edits.add(new
	 * TilesetDimensionEdit(tileset, tileset.getTileWidth(),
	 * tileset.getTileHeight())); tileset.setTileDimensions(tileW, tileH); }
	 * else if (imgChange && !tileDimChange) { edits.add(new
	 * TilesetImageEdit(tileset, tileset.getImagePath()));
	 * tileset.setImagePath(imagePath); } else if (imgChange && tileDimChange) {
	 * edits.add(new TileDimensionAndImageEdit(tileset, tileset .getImagePath(),
	 * tileset.getTileWidth(), tileset .getTileHeight()));
	 * tileset.setImagePath(imagePath); tileset.setTileDimensions(tileW, tileH);
	 * } // fire only a single edit if a single property was edited if
	 * (edits.size() == 1) { fireUndoableEditPerformed(new
	 * GenericChangeEvent<AbstractTilesetEdit>( NewTilesetDialog.this,
	 * edits.get(0))); } // fire a compound edit if more than one property was
	 * edited else { CompoundTilesetEdit compoundEdit = new
	 * CompoundTilesetEdit(tileset, edits, "Tileset Properties");
	 * fireUndoableEditPerformed(new GenericChangeEvent<AbstractTilesetEdit>(
	 * NewTilesetDialog.this, compoundEdit)); } }
	 */

	public void addTilesetEditListener(TilesetEditListener listener) {
		editListeners.add(listener);
	}

	public boolean removeTilesetEditListener(TilesetEditListener listener) {
		return editListeners.remove(listener);
	}

	public void clearTilesetEditListeners() {
		editListeners.clear();
	}
}